package com.zfj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfj.entity.EduTeacher;
import com.zfj.entity.vo.TeacherQuery;
import com.zfj.utils.R;
import com.zfj.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zfj
 * @since 2022-10-03
 */
@Slf4j
@RestController
@RequestMapping("/eduService/edu-teacher")
@Api(tags = "讲师模块")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    // 查询讲师表的数据
    @ApiOperation(value = "查询讲师表的所有数据")
    @GetMapping("/findAll")
    public R findAllTeacher(){

        List<EduTeacher> list = teacherService.list(null);
        return  R.ok().data("items",list);

    }

    //根据id删除
    @ApiOperation(value = "逻辑删除讲师数据")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable("id") String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //分页查询讲师方法
    @ApiOperation(value = "分页查询讲师")
    @GetMapping(value = "/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current,@PathVariable("limit") Long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        teacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合

        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    //条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping(value = "/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") Long current, @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        // @RequestBody(required = false) 参数可为空
//        int i=10/0;
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件查询
        if(!StringUtils.isEmpty(teacherQuery.getName())){
            wrapper.like("name",teacherQuery.getName());
        }
        if(!StringUtils.isEmpty(teacherQuery.getLevel())){
            wrapper.eq("level",teacherQuery.getLevel());
        }
        if(!StringUtils.isEmpty(teacherQuery.getBegin())){
            wrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if(!StringUtils.isEmpty(teacherQuery.getEnd())){
            wrapper.le("gmt_modified",teacherQuery.getEnd());
        }

        wrapper.orderByDesc("gmt_create");

        //调用方法实现分页
        teacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);

        return R.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据讲师id进行修改
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        log.info("打印成功");
        return R.ok().data("teacher",eduTeacher);
    }

    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

